package com.tensquare.friend.controller;

import com.tensquare.friend.client.UserClient;
import com.tensquare.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @BelongsProject: tensquare_parent
 * @BelongsPackage: com.tensquare.friend.controller
 * @Author: yang
 * @CreateTime: 2019-03-13 13:20
 * @Description: ${Description}
 */

@RestController
@RequestMapping("/friend")
@SuppressWarnings("all")
public class FriendController {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private FriendService friendService;

    @Autowired
    private UserClient userClient;

    /**
     * 添加好友或者添加非好友
     * @return
     */
    @RequestMapping(value = "/like/{friendid}/{type}",method = RequestMethod.PUT)
     public Result addFriend(@PathVariable String friendid,@PathVariable String type){
         //验证是否登录，并且拿到当前登录的用户的id
        Claims claims= (Claims) request.getAttribute("claims_user");

        if(claims==null){
            //说明当前用户没有user 角色 就没有权限
            return new Result(true,StatusCode.LOGINERROR,"权限不足");
        }
        //得到当前用户登录的id
        String userid=claims.getId();
        if(type!=null){
            if("1".equals(type)){
                //添加好友
                int flag=friendService.addFriend(userid,friendid);
                if(flag==0){
                    return  new Result(false, StatusCode.ERROR,"不能重复添加好友");
                }
                if(flag==1){
                    //更新好友粉丝数 更新自己关注数 调的userconroller里的方法
                    userClient.updateFanscountAndFollowcount(userid,friendid,1);
                    return new Result(true,StatusCode.OK,"添加成功");
                }
            }
            else if("2".equals(type)){
                //添加非好友
                int flag=friendService.addNoFriend(userid,friendid);
                if(flag==0){
                    return  new Result(false, StatusCode.ERROR,"不能重复添加非好友");
                }
                if(flag==1){
                    return new Result(true,StatusCode.OK,"添加成功");
                }
            }
                return  new Result(false, StatusCode.ERROR,"参数异常");
        }
        else{
            return new Result(false,StatusCode.ERROR,"参数异常");
        }
         //判断是添加好友，还是添加非好友
     }

    /**
     * 删除好友
     * @return
     */
     @RequestMapping(value = "/{friendid}",method = RequestMethod.DELETE)
     public Result deleteFriend(@PathVariable String friendid){
         //验证是否登录，并且拿到当前登录的用户的id
         Claims claims= (Claims) request.getAttribute("claims_user");

         if(claims==null){
             //说明当前用户没有user 角色 就没有权限
             return new Result(true,StatusCode.LOGINERROR,"权限不足");
         }
         //得到当前用户登录的id
         String userid=claims.getId();
         friendService.deleteFriend(userid,friendid);
         userClient.updateFanscountAndFollowcount(userid,friendid,-1);//取消关注 关注数-1 好友粉丝数-1
         return new Result(true,StatusCode.OK,"删除成功");
     }

}
